package com.jz.community.moduleshopping.refund.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.refund.bean.UploadImageBean;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Target on 2018/4/5.
 */
public class OrderUploadImageTask extends RxTask<ArrayList<String>, Integer, UploadImageBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;
    private boolean isNeedDialog;

    public OrderUploadImageTask(Activity activity, boolean isNeedDialog,
                                ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.isNeedDialog = isNeedDialog;
        this.taskListener = taskListener;
    }

    @Override
    protected UploadImageBean doInBackground(ArrayList<String>... params) {
        // 执行请求
        String result = OkHttpUtil.postMultiFile(Constant.ORDER_UPLOAD_IMAGE, new HashMap<>(), "uploadPic", params[0]);
        UploadImageBean bean = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            bean = JsonUtils.parseObject(result, UploadImageBean.class);
        }
        return bean;
    }

    @Override
    protected void onPostExecute(UploadImageBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        if (isNeedDialog) {
            ProgressDialogManager.showDialog(activity);
        }
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

}
